package com.clever.springssm.service.impl;

import com.clever.springssm.entity.Carts;
import com.clever.springssm.exception.ServiceException;
import com.clever.springssm.mapper.CartsMapper;
import com.clever.springssm.request.CartsRequestParams;
import com.clever.springssm.service.CartsService;
import com.clever.springssm.utils.InfoStatus;
import com.clever.springssm.utils.MyBeanUtil;
import com.clever.springssm.vo.CartsVo;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenWang
 * @className CartsServiceImpl
 * @date 2020/10/30 14:53
 * @since JDK 1.8
 */
@Service
public class CartsServiceImpl implements CartsService {
    @Resource
    private CartsMapper cartsMapper;
    @Override
    public int saveCarts(CartsRequestParams cartsRequestParams) {
        //总:设计请求参数根持久化对象的转换问题
        int temp = 0;
        try {
            //判断用户的购物车中是否存在该条记录
            Carts carts = cartsMapper.selectByUserIdAndProductId(cartsRequestParams.getUserId(), cartsRequestParams.getProductId());
            if (carts != null) {
                int num = carts.getQuantity() + cartsRequestParams.getQuantity();
                temp = cartsMapper.updateById(carts.getCartsId(), num);
            } else {
                carts = new Carts();
                /**
                 * 将对想的属性拷贝到另一个对象
                 * 使用工具类进行copy
                 * 注意拷贝的对象要与被拷贝的目标的参数要保持一致
                 * 参数
                 * 参数1 要拷贝的对象
                 * 参数2 目标对象
                 */
                BeanUtils.copyProperties(cartsRequestParams, carts);
                temp = cartsMapper.insert(carts);
            }
        } catch (Exception e) {
            throw new ServiceException(InfoStatus.SERVICE_ERROR.getMessage(), InfoStatus.SERVICE_ERROR.getStatus());
        }
        return temp;
    }

    /**
     * 根据用户ID查询所有的购物车信息
     * @param uid   指定的用户ID
     * @return  List<CartsVo> 返回用于展示的数据的集合
     * @author ChenWang
     * @date 2020/10/30 17:56
     */
    @Override
    public List<CartsVo> seletAllByUserId(int uid){
        List<Carts> cartsList = cartsMapper.selectAllByUserId(uid);
        return MyBeanUtil.copyListProperties(cartsList, CartsVo::new);
    }

    @Override
    public int delCarsById(List<Integer> cartIds) {
        int affect = 0;
        try{
            affect = cartsMapper.delCartsById(cartIds);
        }catch (Exception e){
            throw new ServiceException(InfoStatus.SERVICE_ERROR.getMessage(), InfoStatus.SERVICE_ERROR.getStatus());
        }
        return affect;
    }

    @Override
    public int update(List<CartsRequestParams> cartsRequestParamsList) {
        int affect = 0;
        List<Carts> cartsList = MyBeanUtil.copyListProperties(cartsRequestParamsList, Carts::new);
        try{
            for (Carts carts:cartsList){
                cartsMapper.updateById(carts.getCartsId(),carts.getQuantity());
                affect++;
            }
        }catch (Exception e){
            throw new ServiceException(InfoStatus.SERVICE_ERROR.getMessage(), InfoStatus.SERVICE_ERROR.getStatus());
        }
        return affect;
    }
}
